package com.demo.employees.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

import com.demo.employees.model.Employee;
import com.demo.employees.service.EmployeeService;

@Controller
@RequestMapping("/springMVC")
public class EmployeeController {
	
	@Resource(name="EmployeeServiceDataJpaImpl")
	private EmployeeService employeeService;
	
	@RequestMapping(value="/ListEmployee",method=RequestMethod.GET)
	public String listEmployee(Map<String,List<Employee>> model) {
		List<Employee> list = new ArrayList<Employee>();
		try {
			list = employeeService.findAll();
		} catch (Exception e) {
			e.printStackTrace();
			return UrlBasedViewResolver.REDIRECT_URL_PREFIX + "/springMVC/error?errMsg="+e.getMessage();
		}
		model.put("list", list);
		return "employee/employees";
	}
	
	@RequestMapping(value="/AddEmployee",method=RequestMethod.GET)
	public String addEmployee() {
		return "employee/addEmployee";
	}
	
	@RequestMapping(value="/AddEmployee",method=RequestMethod.POST)
	public String addEmployee(Employee employee) {
		try {
			employeeService.saveEmployee(employee);
		} catch (Exception e) {
			e.printStackTrace();
			return UrlBasedViewResolver.REDIRECT_URL_PREFIX + "/springMVC/error?errMsg="+e.getMessage();
		}
		return UrlBasedViewResolver.REDIRECT_URL_PREFIX + "/springMVC/ListEmployee";
	}
	
	@RequestMapping(value="/UpdateEmployee",method=RequestMethod.GET)
	public String updateEmployee(@RequestParam(value="id")Long id,Map<String,Employee> model) {
		Employee employee = new Employee();
		try {
			employee = employeeService.getById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		model.put("employee", employee);
		return "employee/editEmployee";
	}
	
	@RequestMapping(value="/UpdateEmployee",method=RequestMethod.POST)
	public String updateEmployee(Employee employee) {
		try {
			employeeService.updateEmployee(employee);
		} catch (Exception e) {
			e.printStackTrace();
			return UrlBasedViewResolver.REDIRECT_URL_PREFIX + "/springMVC/error?errMsg="+e.getMessage();
		}
		return UrlBasedViewResolver.REDIRECT_URL_PREFIX + "/springMVC/ListEmployee";
	}
	
	
	@RequestMapping(value="/DelEmployee",method=RequestMethod.GET)
	public String DelEmployee(Long id) {
		try {
			employeeService.deleteById(id);
		} catch (Exception e) {
			e.printStackTrace();
			return UrlBasedViewResolver.REDIRECT_URL_PREFIX + "/springMVC/error?errMsg="+e.getMessage();
		}		
		return UrlBasedViewResolver.REDIRECT_URL_PREFIX + "/springMVC/ListEmployee";
	}
	
	@RequestMapping(value="/error",method=RequestMethod.GET)
	public String error(Long id) {
		return "employee/error";
	}	
}
